package com.bds.snatch.system.util;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * Created by ebin on 2017/8/7.
 */
public class PhantomjsUtilV2 {

	public PhantomjsUtilV2() {
	}

	public synchronized static WebDriver getDriver() {

		try {
			// 关闭程序和脚本
			Runtime runtime = Runtime.getRuntime();

			runtime.exec("taskkill /f /im phantomjs.exe");
			Thread.sleep(1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("初始化phantomjs driver:getWebDriver");

		System.setProperty("phantomjs.binary.path", Const.PHANTOMJS_PATH);

		List<String> cmdList = new ArrayList<String>();
		
		// 本地缓存
		cmdList.add("--disk-cache=true");
	
		DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs();
		desiredCapabilities.setJavascriptEnabled(true);
		desiredCapabilities.setCapability("phantomjs.page.settings.userAgent",
				"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
						+ "Chrome/59.0.3071.82 Safari/537.36");
		desiredCapabilities.setCapability("phantomjs.cli.args", cmdList);
		return new PhantomJSDriver(desiredCapabilities);

	}

	

	

	public static void main(String[] args) throws Exception {
		Const.POINT = new LinkedList<Integer>();
		for (int i = 1; i <= 3; i++) {
			Const.POINT.offer(i);
		}
		Const.PHANTOMJS_PATH = "D:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe";

		Thread.sleep(3000);

		Thread thread1 = new Thread(new Runnable() {
			@Override
			public void run() {
				Integer name = 1;

				// webdriver指针指定获取哪一个webdriver
				WebDriver driver = PhantomjsUtilV2.getDriver();

				driver.get("http://www.gc-zb.com/Info/lists.html?page=1&pid=10");
				System.out.println(driver.getTitle());
				//driver.quit();
			}
		});

		Thread thread2 = new Thread(new Runnable() {
			@Override
			public void run() {
				Integer name = 2;

				WebDriver driver = PhantomjsUtilV2.getDriver();
				driver.get("https://www.oschina.net/p/t-io/comments?fromerr=4ZeynBoZ");
				System.out.println(driver.getTitle());
				//driver.quit();
			}
		});

		Thread thread3 = new Thread(new Runnable() {
			@Override
			public void run() {
				Integer name = 3;

				WebDriver driver = PhantomjsUtilV2.getDriver();
				driver.get("http://qq.news.com/");
				System.out.println(driver.getTitle());
				driver.quit();
			}
		});

		Thread thread4 = new Thread(new Runnable() {
			@Override
			public void run() {
				Integer name = 4;

				WebDriver driver = PhantomjsUtilV2.getDriver();
				driver.get("http://bbs.bato.cn/thread-5525951-1-1.html");
				System.out.println(driver.getTitle());
				driver.quit();
			}
		});

		Thread thread5 = new Thread(new Runnable() {
			@Override
			public void run() {
				Integer name = 5;

				WebDriver driver = PhantomjsUtilV2.getDriver();
				driver.get("https://bigdata.163yun.com/youdata?channel=B_baidu_bi_00016");
				System.out.println(driver.getTitle());
				driver.quit();
			}
		});

		// thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
		thread5.start();

	}
}
